import { ElLoading, ElMessage } from 'element-plus'
import { Router } from 'vue-router'

export default class System {
    /**
     * 路由
     */
    static router: Router

    static er = (content: string, options: any = {}) => {
        const optionObj = Object.assign(
            {
                message: content,
                type: 'error'
            },
            options
        )
        ElMessage(optionObj)
    }

    static sr = (content: string, options: any = {}) => {
        const optionObj = Object.assign(
            {
                message: content,
                type: 'success'
            },
            options
        )
        ElMessage(optionObj)
    }

    static isloading = null as any
    private static loadObjTimer = null as any

    /**
     * 显示和隐藏加载
     * @param show
     */
    static loading = (show: boolean, timeout: number = 30000) => {
        if (show) {
            if (!this.isloading) {
                this.isloading = ElLoading.service({
                    background: 'rgba(0, 0, 0, 0.5)',
                    customClass: 'fullscreenLoading'
                })
                this.loadObjTimer = Timer.once(() => {
                    this.loading(false)
                }, timeout)
            }
        } else {
            Timer.un(this.loadObjTimer)
            this.loadObjTimer = null
            this.isloading?.close()
            this.isloading = null
        }
    }
}
